package com.superteam.zzl.viewmodel.DynamicFragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.ActivityInfoBinding;
import com.superteam.zzl.adapter.InfoActivityAdapter;
import com.superteam.zzl.model.DynamicFragment.MInfo;
import com.superteam.zzl.model.DynamicFragment.bean.Info;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/14.
 */

public class InfoPresenter implements Callback<Object> {

    private Context context;
    private ActivityInfoBinding db;
    private MInfo mInfo;
    private InfoActivityAdapter adpater;
    private List<Object> list;
    private Info info;

    public InfoPresenter(Context context, ActivityInfoBinding db) {
        this.context = context;
        this.db = db;
        mInfo = new MInfo(this);
    }

    @Override
    public void onResponse(Call<Object> call, Response<Object> response) {
        Object body = response.body();
        if (body instanceof Info) {
            info = (Info) body;
            setHeadView();
            if (list.size() > 0) {
                if (info.data.trends != null) {
                    list.add(0, info.data.trends);
                    if (info.data.seller != null) {
                        list.add(1, info.data.seller);
                    }
                } else if (info.data.seller != null) {
                    list.add(0, info.data.seller);
                }
            } else {
                if (info.data.trends != null) {
                    list.add(info.data.trends);
                } else if (info.data.seller != null) {
                    list.add(info.data.seller);
                }
            }
        }
    }

    private void setHeadView() {
        db.setUser(info.data.user);
        if (!info.data.user.gender.equals("m")) {
            Drawable drawable;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                drawable = context.getResources().getDrawable(R.drawable.profile_female, null);
            } else {
                drawable = context.getResources().getDrawable(R.drawable.profile_female);
            }
            db.gender.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        }
        BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
        Glide.with(context).load(info.data.user.avatar).bitmapTransform(new CropCircleTransformation(bitmapPool)).into(db.iv);
    }

    @Override
    public void onFailure(Call<Object> call, Throwable t) {

    }

    public void init() {
        list = new ArrayList<>();
        adpater = new InfoActivityAdapter(context, list);
    }

    public void initView() {
        db.tl.setNavigationIcon(R.drawable.back1);
        db.rv.setAdapter(adpater);
    }

    public void loadData(String id) {
        mInfo.loadDada(id);
    }
}
